package com.example.villagemanager.ui.fragment.person;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

import com.example.villagemanager.R;
import com.example.villagemanager.config.Constant;
import com.example.villagemanager.listener.OnItemClickListener;
import com.example.villagemanager.manager.DialogManager;
import com.example.villagemanager.model.bean.Person;
import com.example.villagemanager.model.entity.BaseResponse;
import com.example.villagemanager.model.event.Event;
import com.example.villagemanager.model.response.PersonPagesResponse;
import com.example.villagemanager.network.BaseObserver;
import com.example.villagemanager.network.RetrofitHelper;
import com.example.villagemanager.network.RxUtil;
import com.example.villagemanager.network.api.AppInfoApi;
import com.example.villagemanager.ui.activity.PersonDetialActivity;
import com.example.villagemanager.ui.adapter.PersonAdapter;
import com.example.villagemanager.ui.base.LazyloadFragment;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import butterknife.BindView;

public class PermanentFragment extends LazyloadFragment {
    private static final String NAME = "name";
    private static final String TAG = PermanentFragment.class.getSimpleName();


    @BindView(R.id.x_recycler_view)
    XRecyclerView xRecyclerView;


    List<Person> personList=new ArrayList<>();
    PersonAdapter adapter;
    private int regionId=0;
    private int  pageNum=1;
    private int  pageSize=20;
    private Person person;
    private boolean isRefresh=true;
    @Override
    protected int setContentView() {
        return R.layout.fragment_permanent;
    }

    @Override
    protected void init() {
        xRecyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
         adapter=new PersonAdapter(this.getContext(),personList);
        xRecyclerView.setAdapter(adapter);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pageNum=1;
                isRefresh=true;
                httpgetPersonPage();

            }

            @Override
            public void onLoadMore() {
                pageNum++;
                isRefresh=false;
                httpgetPersonPage();

            }
        });
        adapter.setmOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                person=personList.get(position);
                showEditDialog();

            }
        });



    }

    private void showEditDialog() {
        DialogManager.showListAlertDialog(this.getContext(),"请选择操作", Constant.dialogEditTitles, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case 0:
                        Toast.makeText(PermanentFragment.this.getContext(),"查看",Toast.LENGTH_SHORT).show();
                        break;
                    case 1:

                        Toast.makeText(PermanentFragment.this.getContext(),"编辑",Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(PermanentFragment.this.getContext(),"删除",Toast.LENGTH_SHORT).show();
                        break;
                }
                if (person==null){

                }else {
                    if (mBundle==null){
                        mBundle=new Bundle();
                    }
                    mBundle.putInt(Constant.PERSON_ID,person.getId());
                    sendIntent(PersonDetialActivity.class,mBundle);
                }

            }

        });
    }

    @Override
    protected void lazyLoad() {
        httpgetPersonPage();


    }

    private void httpgetPersonPage() {
        RetrofitHelper.create(AppInfoApi.class)
                .personPage(regionId,pageNum,pageSize)
                .compose(RxUtil.observervableToMain())
                .as(RxUtil.bindLifecycle( this))
                .subscribe(new BaseObserver<PersonPagesResponse>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<PersonPagesResponse> response) {
                        if (response.getData().getList()!=null) {
                            if (isRefresh){
                                personList.clear();
                                xRecyclerView.refreshComplete();
                            }else {
                                xRecyclerView.loadMoreComplete();
                            }
                            personList.addAll(response.getData().getList());
                            adapter.notifyDataSetChanged();





                        }
                    }
                });
    }

    @Override
    protected void onEventProcess(Event event) {


    }

    @Override
    public void InitLazy() {

    }
}
